﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WebApiMIT3.DatabaseAccessLayer;
using WebApiMIT3.Models;
using WebApiMIT3.Utils;

namespace WebApiMIT3.Database
{
    public class SQLManager
    {
        private readonly PoliticalInfo politicalInfo;
        private readonly CategoryMN categoryMN;
        private readonly MultiMediaMN multiMediaMN;
        private readonly TypeMN typeMN;

        public SQLManager()
        {
            this.politicalInfo = new PoliticalInfo();
            this.categoryMN = new CategoryMN();
            this.multiMediaMN = new MultiMediaMN();
            this.typeMN = new TypeMN();
        }

        public async Task AddModelToDatabase(DataModel dataModel)
        {
            await Task.Run(() =>
            {
                DataModel tempDataModel = politicalInfo.SelectIndexAndDescriptions(dataModel.SourceUrl);
                if (string.IsNullOrEmpty(tempDataModel.InfoID))
                {
                    this.politicalInfo.InsertStatement(dataModel);
                    this.categoryMN.InsertStatement(dataModel);
                    this.typeMN.InsertStatement(dataModel);
                    this.multiMediaMN.InsertStatement(dataModel);
                }
                else
                {
                    if (!string.IsNullOrEmpty(tempDataModel.FullDescription))
                    {
                        if (CompareUtil.IsMoreThan5ProcentDifferent(tempDataModel.FullDescription, dataModel.FullDescription))
                        {
                            UpdateData(tempDataModel, dataModel);
                        }
                    }
                    else
                    {
                        if (CompareUtil.IsMoreThan5ProcentDifferent(tempDataModel.SummaryDescription, dataModel.SummaryDescription))
                        {
                            UpdateData(tempDataModel, dataModel);
                        }
                    }
                }
            });
        }

        public async Task<List<DataModel>> GetModelFromDatabase()
        {
            var dataModelList = new List<DataModel>();

            await Task.Run(() =>
            {
                dataModelList = this.politicalInfo.AddToModel(dataModelList);
                dataModelList = this.categoryMN.SelectCategories(dataModelList);
                dataModelList = this.typeMN.SelectTypes(dataModelList);
                dataModelList = this.multiMediaMN.SelectMultiMedia(dataModelList);
            });
            return dataModelList;
        }
        
        private void UpdateData(DataModel tempDataModel, DataModel dataModel)
        {
            this.politicalInfo.UpdateStatement(dataModel, tempDataModel.InfoID, false);
            this.categoryMN.UpdateStatement(dataModel, tempDataModel.InfoID);
            this.typeMN.UpdateStatement(dataModel, tempDataModel.InfoID);
            this.multiMediaMN.UpdateStatement(dataModel, tempDataModel.InfoID);
        }
    }
}